package com.kitty.game.utils;

import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Calendar;
import java.util.Date;

/**
 * 日期格式化工具类
 */
public class DateTimeUtil {

    /**
     * 格式 yyyy年MM月dd日 HH:mm:ss
     *
     * @param dateTime
     * @return
     */
    public static String getDateTimeDisplayString(LocalDateTime dateTime) {
        DateTimeFormatter dtf2 = DateTimeFormatter.ofPattern("yyyy年MM月dd日 HH:mm:ss");
        String strDate2 = dtf2.format(dateTime);

        return strDate2;
    }

    /*
     * 将时间戳转换为时间
     */
    public static String stampToDate(String s){
        String res;
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        long lt = new Long(s);
        Date date = new Date(lt);
        res = simpleDateFormat.format(date);
        return res;
    }
    /**
     *
     * @return
     * @description 得到当前时间 2015-11-20 14:41:19
     * @version currentVersion
     */
    public static String getCurrentTimHHmm() {
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm");
        String createTime = formatter.format(new Date());
        return createTime;
    }
    /**
     *
     * @return
     * @description 得到当前时间 2015-11-20 14:41:19
     * @version currentVersion
     */
    public static String getCurrentDay() {
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
        String createTime = formatter.format(new Date());
        return createTime;
    }
    /**
     *
     * @return
     * @description 得到当前时间 2015-11-20 14:41:19
     * @version currentVersion
     */
    public static int getDD() {
        SimpleDateFormat formatter = new SimpleDateFormat("dd");
        String createTime = formatter.format(new Date());
        return Integer.valueOf(createTime);
    }
    public static String getCurrentTimHHmmss() {
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String createTime = formatter.format(new Date());
        return createTime;
    }
    public static String getCurrentTimHHmmssWithPay() {
        SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMddHHmmss");
        String createTime = formatter.format(new Date());
        return createTime;
    }
    /**
     * Date类型转换为10位时间戳
     * @return
     */
    public static Integer getSysTimestamp(){
        Timestamp ts = new Timestamp(System.currentTimeMillis());

        return (int) ((ts.getTime())/1000);
    }
    /**
     * 判断当前时间是否在[startTime, endTime]区间，注意时间格式要一致
     *
     * @author jqlin
     */
    public static boolean isEffectiveDate(String starttime, String endtime) {
        try {

            String format = "yyyy-MM-dd HH:mm:ss";
            Date startTime = new SimpleDateFormat(format).parse(starttime);
            Date endTime = new SimpleDateFormat(format).parse(endtime);
            Date nowTime=new Date();
            if (nowTime.getTime() >= startTime.getTime()
                    && nowTime.getTime() <=endTime.getTime()) {
                return true;
            }else{
                return false;
            }

        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    public static long getTime(Date date){
//        long time = (new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")).parse("2018-06-30 20:00:00", new ParsePosition(0)).getTime() / 1000;
        return date.getTime() / 1000;

    }

    /**
     * 判断时间是否在时间段内
     *
     * @param nowTime
     * @param beginTime
     * @param endTime
     * @return
     */
    public static boolean belongCalendar(Date nowTime, Date beginTime,
                                         Date endTime) {
        Calendar date = Calendar.getInstance();
        date.setTime(nowTime);

        Calendar begin = Calendar.getInstance();
        begin.setTime(beginTime);

        Calendar end = Calendar.getInstance();
        end.setTime(endTime);

        if (date.after(begin) && date.before(end)) {
            return true;
        } else {
            return false;
        }
    }

    public static int diffDays(String day1, String day2) {
        try {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyymmdd");
            Date d1 = sdf.parse(day1);
            Date d2 = sdf.parse(day2);
//            System.out.println("time1:"+ d1.getTime());
//            System.out.println("time2:"+ d2.getTime());
            long daysBetween = (d2.getTime() - d1.getTime())/(60*60*24*1000);
            return (int) daysBetween;
        }
        catch (Exception e){

        }
        return 0;

    }

    public static void main(String args[]) throws ParseException {
        boolean flag=isEffectiveDate("2021-08-17 00:05:20","2021-08-17 01:07:00");
        System.out.println(flag);
        System.out.println(getCurrentDay());
        System.out.println(getCurrentTimHHmmss());

        String startTimeStr = "2022-01-08 13:53:00";
        String format = "yyyy-MM-dd HH:mm:ss";
        Date startTime = new SimpleDateFormat(format).parse(startTimeStr);
        Date now = new Date();

        System.out.println(startTime.getTime());
        System.out.println(now.getTime());
        System.out.println(now.getTime() > startTime.getTime());
        if(now.getTime() < startTime.getTime()){
            System.out.println("时间未到----");
        }

    }
}
